package com.itheima.day2.hiorder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class C04SimpleStream01<T> {

    private Collection<T> collection;

    private C04SimpleStream01(Collection<T> collection) {
        this.collection = collection;
    }

    public static <T> C04SimpleStream01<T> of(Collection<T> collection) {
        return new C04SimpleStream01<>(collection);
    }

    public C04SimpleStream01<T> filter(Predicate<T> predicate) {
        List<T> result = new ArrayList<>();
        for (T t : collection) {
            if (predicate.test(t)) {
                result.add(t);
            }
        }
        return new C04SimpleStream01<>(result);
    }

    public <U> C04SimpleStream01<U> map(Function<T, U> function) {
        List<U> result = new ArrayList<>();
        for (T t : collection) {
            result.add(function.apply(t));
        }
        return new C04SimpleStream01<>(result);
    }

    public void forEach(Consumer<T> consumer) {
        for (T t : collection){
            consumer.accept(t);
        }
    }

    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);

        C04SimpleStream01.of(list)
            .filter(value -> value % 2 == 0)
            .map(value -> value * value)
            .forEach(System.out::println);
    }

}
